package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"sys-manager/cache"
	"sys-manager/models"
	"time"
)

func Add(c *gin.Context) {
	carName := c.PostForm("carName")
	carNum := c.PostForm("carNum")
	carPrice := c.PostForm("carPrice")

	if carName == "" || carNum == "" || carPrice == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Car{
		CarName:  carName,
		CarNUm:   carNum,
		CarPrice: carPrice,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	orderCode := c.PostForm("orderCode")
	orderName := c.PostForm("orderName")
	orderXi := c.PostForm("orderXi")

	if orderCode == "" || orderName == "" || orderXi == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Order{
		OrderCode: orderCode,
		OrderName: orderName,
		OrderXi:   orderXi,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "添加成功",
	})
	return
}

func CarList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))

	offset := (page - 1) * size
	var news []models.CarOrderList
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_name as CarName,
		  cars.car_n_um as CarNUm,
		  cars.car_price as CarPrice,
		  orders.order_code as OrderCode,
		  orders.order_name as OrderName,
		  orders.order_xi as OrderXi
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.id
	*/
	redis_key := "List_" + strconv.Itoa(page)
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if errCount == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_n_um as CarNUm",
				"cars.car_price as CarPrice",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_xi as OrderXi",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.id").
			Offset(offset).
			Limit(size).
			Find(&news)
		jsoData, _ := json.Marshal(news)
		cache.Rdb.Set(cache.Ctx, redis_key, jsoData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(errCount), &news)
	}

	var count int64
	models.DB.Model(&models.Car{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  news,
		"count": count,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	var news models.CarOrderList

	redis_key := "detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_n_um as CarNUm",
				"cars.car_price as CarPrice",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_xi as OrderXi",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.id").
			Where("orders.id", id).
			Debug().
			Find(&news)
		jsonData, _ := json.Marshal(news)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &news)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}
